﻿using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;

namespace TMSIM.Client.BizGIS.Business
{
    public class GeoDatabase
    {
        /// <summary>
        /// 描述：在内存创建一个Workspace
        /// </summary>
        /// <param name="workspaceName">Workspace Name</param>
        /// <returns>IWorkspace</returns>
        public IWorkspace CreateInMemoryWorkspace(string workspaceName)
        {
            try
            {
                IWorkspaceFactory pWSF = new InMemoryWorkspaceFactoryClass();
                IWorkspaceName pWSN = pWSF.Create("", workspaceName, null, 0);
                IName name = (IName)pWSN;
                IWorkspace pWS = (IWorkspace)name.Open();
                return pWS;
            }
            catch (Exception ex)
            {
                Console.WriteLine("调用CreateInMemoryWorkspace出错，" + ex.Message.ToString());
                return null;
            }
        }

        /// <summary>
        /// 描述：在内存创建一个临时图层

        /// </summary>
        /// <returns>IFeatureClass</returns>
        public IFeatureClass CreateInMemoryFeatureClass()
        {
            try
            {
                IFields pFields = new FieldsClass();
                IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

                IField pField = new FieldClass();
                IFieldEdit pFieldEdit = (IFieldEdit)pField;

                //设置Field的属性 
                pFieldEdit.Name_2 = FieldNameConst.OBJECTID;
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;

                //添加一个Field
                pFieldsEdit.AddField(pField);
                pField = null;

                //创建shapeField
                ESRI.ArcGIS.Geometry.ISpatialReferenceFactory2 pSpatRefFact = new ESRI.ArcGIS.Geometry.SpatialReferenceEnvironmentClass();
                ESRI.ArcGIS.Geometry.IGeographicCoordinateSystem pGeoCoordSys = pSpatRefFact.CreateGeographicCoordinateSystem(4326);

                pGeoCoordSys.SetDomain(-399.999999998795, 8679577.71248047, -399.999999997677, 8679577.71248047);
                pGeoCoordSys.SetFalseOriginAndUnits(-399.999999998795, -399.999999998795, 100000);

                IGeometryDef pGeoDef = new GeometryDefClass();
                IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;

                pGeoDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint;
                pGeoDefEdit.GridCount_2 = 1;
                pGeoDefEdit.set_GridSize(0, 1000);
                pGeoDefEdit.SpatialReference_2 = pGeoCoordSys;

                pField = new FieldClass();
                pFieldEdit = (IFieldEdit)pField;

                pFieldEdit.Name_2 = FieldNameConst.Shape;
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                pFieldEdit.GeometryDef_2 = pGeoDef;

                pFieldsEdit.AddField(pField);

                pSpatRefFact = null;
                pField = null;

                //其他属性

                pField = new FieldClass();
                pFieldEdit = (IFieldEdit)pField;
                pFieldEdit.Name_2 = FieldNameConst.CarPlate;
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                pFieldEdit.Length_2 = 100;

                pFieldsEdit.AddField(pField);

                pField = new FieldClass();
                pFieldEdit = (IFieldEdit)pField;
                pFieldEdit.Name_2 = FieldNameConst.CarType;
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                pFieldEdit.Length_2 = 100;

                pFieldsEdit.AddField(pField);

                pField = new FieldClass();
                pFieldEdit = (IFieldEdit)pField;
                pFieldEdit.Name_2 = FieldNameConst.Longitude;
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;

                pFieldsEdit.AddField(pField);

                pField = new FieldClass();
                pFieldEdit = (IFieldEdit)pField;
                pFieldEdit.Name_2 = FieldNameConst.Latitude;
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;

                pFieldsEdit.AddField(pField);

                pField = new FieldClass();
                pFieldEdit = (IFieldEdit)pField;
                pFieldEdit.Name_2 = FieldNameConst.AreaCode;
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                pFieldEdit.Length_2 = 100;

                pFieldsEdit.AddField(pField);

                pField = new FieldClass();
                pFieldEdit = (IFieldEdit)pField;
                pFieldEdit.Name_2 = FieldNameConst.CarFleet;
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                pFieldEdit.Length_2 = 100;

                pFieldsEdit.AddField(pField);

                pField = new FieldClass();
                pFieldEdit = (IFieldEdit)pField;
                pFieldEdit.Name_2 = FieldNameConst.RoadCoding;
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
               
                pFieldsEdit.AddField(pField);

                pField = null;

                IFeatureWorkspace pFWS = (IFeatureWorkspace)CreateInMemoryWorkspace("Workspace");
                IFeatureClass pFC = pFWS.CreateFeatureClass("CarPosition", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");

                return pFC;
            }
            catch (Exception ex)
            {
                Console.WriteLine("调用CreateInMemoryFeatureClass出错，" + ex.Message.ToString());
                return null;
            }
        }

        public IFeatureClass CreateInMemoryFeatureClass(IFields fields,string className,string workspaceName)
        {
            IFeatureWorkspace pFWS = (IFeatureWorkspace)CreateInMemoryWorkspace(workspaceName);
            IFeatureClass pFC = pFWS.CreateFeatureClass(className, fields, null, null, esriFeatureType.esriFTSimple, "Shape", "");

            return pFC;
        }
    }
}
